﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;

namespace LogicUniversity.Employee
{
    public partial class ViewRequistion : System.Web.UI.Page
    {
        CoreHeadFascade ef = new CoreHeadFascade();
        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //if (Session["Role"] as string != "Employee" || Session["Role"] as string != "Representative")
            //{
            //    
            //}
            if (Session["Role"] as string != "Employee")
            {
                if (Session["Role"] as string == "Representative")
                {
                    this.MasterPageFile = "~/Representative/RepMaster.Master";
                }
                else
                {
                    Response.Redirect("~/Login.aspx");
                }
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            

            //if (!IsPostBack)
            //{
            //    GridView1.DataSource = ef.getRequisiton(DateTime.Now, DateTime.Now.AddDays(-7));
            //    GridView1.DataBind();
            //}
        }

        protected void btnFromCal_Click(object sender, EventArgs e)
        {
            //fromCal.Visible = true;
        }

        protected void btnToCal_Click(object sender, EventArgs e)
        {
            //toCal.Visible = true;
        }

        protected void fromCal_SelectionChanged(object sender, EventArgs e)
        {
            //fromDate.Text = fromCal.SelectedDate.ToShortDateString();
            //fromCal.Visible = false;
        }

        protected void toCal_SelectionChanged(object sender, EventArgs e)
        {
            //DateTime from = DateTime.Parse(fromDate.Text.Trim());

            //if (from >toCal.SelectedDate)
            //{
            //    lblError.Text = "'To Date' cannot be greater than 'From Date'";
            //    return;
            //}

            //toDate.Text = toCal.SelectedDate.ToShortDateString();
            //toCal.Visible = false;
            //lblError.Text = null;
            //btnSelect.Enabled = true;
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            DateTime dateFrom = DateTime.Parse(fromDate.Text.Trim());
            DateTime dateTo = DateTime.Parse(toDate.Text.Trim());

            string empID = Session["UserID"] as string;
            string deptCode = ef.getDepartmentCode(empID);
            IQueryable query = ef.getRequisiton(dateFrom, dateTo, empID);
            
            if (dateFrom>dateTo)
            {
                lblError.Text = "'To Date' cannot be less than 'From Date'";
                return;
            }
            if (query==null)
            {
                lblMsg.Text = "Currently No Requests In Selected Date Range";
                return;
                
            }
            
            lblError.Text = null;
            RequestsView.DataSource = query;
            RequestsView.DataBind();

            //ViewState["request"] = query;
        }

        protected void lnkViewDetail_Click(object sender, EventArgs e)
        {
            LinkButton btn = (LinkButton)sender;
            string reqID = btn.CommandArgument.Trim();

            string url = "ViewReqDetail.aspx?reqid=" + reqID;
            Response.Redirect(url);
        }
       
        
    }
}